import { checkStockForSparePartApplyCheck } from '@/api/store'
import { ref } from 'vue'

/**
 * 配件申请单审核前校验库存服务
 */
export default function () {
  const checkStockLoading = ref(false)
  const checkStockResult = ref<{ codeIsSufficientMap: Record<string, number> }>({
    codeIsSufficientMap: {},
  })
  const checkStockParam = ref<Record<string, number>>({})

  const checkStock = async (sparePartApplyId: string) => {
    try {
      checkStockLoading.value = true
      const res = await checkStockForSparePartApplyCheck(sparePartApplyId, checkStockParam.value)
      checkStockLoading.value = false
      if (res.data.code === 20000) {
        checkStockResult.value = res.data.data.row
        return ''
      }

      return res.data.message
    } catch (error) {
      checkStockLoading.value = false
      return '校验库存失败，请稍后重试'
    }
  }

  return {
    checkStockParam,
    checkStockResult,
    checkStockLoading,
    checkStock,
  }
}
